package com.atguigu.eduservice.service.impl;

import com.atguigu.commonutils.vo.commentVo.CommentMemberVo;
import com.atguigu.eduservice.client.UcenterClient;
import com.atguigu.eduservice.entity.EduComment;
import com.atguigu.eduservice.entity.vo.CommentInfoVo;
import com.atguigu.eduservice.mapper.CommentMapper;
import com.atguigu.eduservice.service.CommentService;
import com.atguigu.servicebase.exceptionhandler.GuliException;
import com.atguigu.utils.JwtUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 评论 服务实现类
 * </p>
 *
 * @author AIR
 * @since 2022-10-11
 */
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, EduComment> implements CommentService {

    @Autowired
    private UcenterClient ucenterClient;

    @Override
    public Map<String,Object> getCommentByCourseId(Long current, Long limit,String courseId) {
        Page<EduComment> pageParam = new Page<>(current,limit);

        LambdaQueryWrapper<EduComment> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(EduComment::getCourseId,courseId);
        wrapper.orderByDesc(EduComment::getGmtCreate);

        baseMapper.selectPage(pageParam,wrapper);

        List<EduComment> records = pageParam.getRecords();
        long currentPage = pageParam.getCurrent();
        long pages = pageParam.getPages();
        long size = pageParam.getSize();
        long total = pageParam.getTotal();
        boolean hasNext = pageParam.hasNext();
        boolean hasPrevious = pageParam.hasPrevious();

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("items", records);
        map.put("current", currentPage);
        map.put("pages", pages);
        map.put("size", size);
        map.put("total", total);
        map.put("hasNext", hasNext);
        map.put("hasPrevious", hasPrevious);

        return map;
    }

    //添加评论
    @Override
    public void insertComment(CommentInfoVo commentInfoVo, HttpServletRequest request) {
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        //判断用户是否登录
        if (StringUtils.isBlank(memberId)){
            throw new GuliException(20001,"请先登录");
        }

        //评论内容，课程id，讲师id
        EduComment comment = new EduComment();
        BeanUtils.copyProperties(commentInfoVo,comment);

        //传用户id，远程调用ucenter里mapper查询数据库，查出用户姓名，用户头像
        CommentMemberVo memberInfo = ucenterClient.getMemberInfo(memberId);
        BeanUtils.copyProperties(memberInfo,comment);

        baseMapper.insert(comment);
    }
}
